package com.tensquare.user.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/** 安全配置类. */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

  /**
  * authorizeRequests() -> 所有security全注解配置实现的开端，表示开始说明需要的权限，需要的权限需要两部分，
  *                        1：拦截的路径， 2：访问该路径需要的权限
  * antMatchers() -> 表示拦截路径
  * permitAll()   -> 任何权限都可以访问（通行）
  * anyRequest()  -> 任何的请求
  * authenticated() -> 认证后才能访问
  * .and().csrf().disable() -> 固定写法，表示使 csrf 拦截失效.
  */
  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/**").permitAll()
        .anyRequest()
        .authenticated()
        .and().csrf().disable();
  }

  @Bean
  public BCryptPasswordEncoder encoder() {
    return new BCryptPasswordEncoder();
  }

}
